package com.qkl.common.publics.service;

import com.qkl.common.publics.dto.BusinessDTO;
import com.qkl.common.publics.dto.WXTokenDTO;
import com.qkl.common.publics.dto.WXTokenOutDTO;
import com.qkl.common.publics.entity.WXInfoEntity;
import com.qkl.common.publics.mapper.WXInfoMapper;
import com.qkl.common.publics.utils.PublicSessionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class WXInfoService {

    @Autowired
    private WXInfoMapper WXInfoMapper;

    @Autowired
    private WXService wxService;

    public BusinessDTO<WXTokenOutDTO> getAccessToken(){
        String appId = PublicSessionUtils.getPublicCrypto().getAppId();
        log.info("=========getAccessToken.appId={}",appId);
        BusinessDTO<WXInfoEntity> businessDTO = this.queryWXInfo(appId);
        if(!businessDTO.isSucess()){
            return BusinessDTO.failBusiness(businessDTO.getMessage());
        }
        WXInfoEntity wxInfoEntity =  businessDTO.getBusinessObject();
        if(wxInfoEntity.tokenExpires()){
            return this.refreshAccessToken(wxInfoEntity);
        }
        WXTokenOutDTO outDTO = new WXTokenOutDTO();
        outDTO.setAccessToken(wxInfoEntity.getAccessToken());
        outDTO.setExpiresSeconds(wxInfoEntity.expiresSeconds());
        return BusinessDTO.sucessBusiness(outDTO);
    }

    public BusinessDTO<WXTokenOutDTO>  refreshAccessToken(){
        String appId = PublicSessionUtils.getPublicCrypto().getAppId();
        log.info("=========refreshAccessToken.appId={}",appId);
        BusinessDTO<WXInfoEntity> businessDTO = this.queryWXInfo(appId);
        if(!businessDTO.isSucess()){
            return BusinessDTO.failBusiness(businessDTO.getMessage());
        }
        return this.refreshAccessToken(businessDTO.getBusinessObject());
    }
    public BusinessDTO<WXTokenOutDTO>  refreshAccessToken(WXInfoEntity wxInfoEntity ){
        BusinessDTO<WXTokenDTO> businessDTO = this.requestWXToken(wxInfoEntity);
        WXTokenDTO wxTokenDTO = businessDTO.getBusinessObject();
        if(businessDTO.isSucess() && wxTokenDTO.responseOK()){
            WXInfoEntity updEntity = new WXInfoEntity();
            updEntity.setAppid(wxInfoEntity.getAppid());
            updEntity.setAccessToken(wxTokenDTO.getAccess_token());
            updEntity.setExpiresToken(wxTokenDTO.getExpires_in());
            updEntity.setTokenTime(System.currentTimeMillis());
            updEntity.setUpdateTime(new Date());
            WXInfoMapper.updateById(updEntity);

            WXTokenOutDTO outDTO = new WXTokenOutDTO();
            outDTO.setAccessToken(wxTokenDTO.getAccess_token());
            outDTO.setExpiresSeconds(wxTokenDTO.getExpires_in());
            return BusinessDTO.sucessBusiness(outDTO);
        }
        return BusinessDTO.failBusiness(wxTokenDTO.getErrmsg());
    }

    public BusinessDTO<WXTokenDTO> requestWXToken(WXInfoEntity wxInfoEntity){
        return BusinessDTO.sucessBusiness(wxService.wxToken(wxInfoEntity.getAppid(),wxInfoEntity.getSecret()));
    }

    private BusinessDTO<WXInfoEntity> queryWXInfo(String appId){
        WXInfoEntity wxInfoEntity =  WXInfoMapper.selectById(appId);
        if(null == wxInfoEntity){
            return BusinessDTO.failBusiness("公众号或小程序信息未配置");
        }
        String secret = StringUtils.trimToEmpty(wxInfoEntity.getSecret());
        if(StringUtils.isEmpty(secret)){
            return BusinessDTO.failBusiness("公众号或小程序secret未配置");
        }
        return BusinessDTO.sucessBusiness(wxInfoEntity);
    }

}
